package com.cos.openapi.web.controller;

import com.cos.openapi.common.Const;
import com.cos.openapi.common.ResponseResult;
import com.cos.openapi.common.ResultTable;
import com.cos.openapi.entity.AdminUser;
import com.cos.openapi.entity.Menu;
import com.cos.openapi.entity.Role;
import com.cos.openapi.services.MenuServices;
import com.cos.openapi.services.RoleServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author: COS
 * @time: 2021/11/24 14:22
 * @description:
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleServices roleServices;
    @Autowired
    private MenuServices menuServices;

    @RequestMapping("/role_tree")
    public List<Role> getAllRole(HttpServletRequest request){

        return roleServices.getAllRole();
    }


    @RequestMapping("/getRole")
    public ResultTable getRolePageWithName(@RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "10") Integer limit,
                                           String name) {
        ResultTable rt = roleServices.getRolePageWithName(page, limit, name);
        return rt;
    }

    @RequestMapping("/updateStatus")
    public ResponseResult updateStatus(Integer id, Integer status) {
        return roleServices.updateStatus(id, status);
    }
    @RequestMapping("/update")
    public ResponseResult update(Role r) {
        return roleServices.updateRole(r);
    }
    @RequestMapping("/add")
    public ResponseResult add(Role r) {
        return roleServices.add(r);
    }

    @RequestMapping("/role_menu")
    public List<Integer> getRoleMenu(Integer roleId) {
        return roleServices.getRoleMenu(roleId);
    }

    @RequestMapping("/menu_tree")
    public List<Menu> getMenuTree() {
        return menuServices.getAllMenuTree();
    }

    @RequestMapping("/assign_menu")
    public ResponseResult assignMenu(Integer roleId, Integer[] menuIds) {
        try {
            return roleServices.assignMenu(roleId, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("操作失败");
    }

}
